﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using System.Data;
using NetBay.Core.Extensions;
using Rio.Framework.Historical;

#endregion


namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Organization database row to Organization object
    /// </summary>
    public class OrganizationTranslator
    {
        /// <summary>
        /// Translate a bunch of Organization DataReader to Organization object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Organization Object</returns>
        public static List<Organization> TranslateOrganization(IDataReader P_oDbDataReader)
        {
            var l_lstOrganization = new List<Organization>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("OrganizationTranslator.TranslateOrganization (P_oDbDataReader <" + P_oDbDataReader.ToString() + ">)...");

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        var l_oOrganization = new OrganizationRepository();

                        l_oOrganization.Identifier = Convert.ToInt32(P_oDbDataReader["RE_ID"].ToString());
                        l_oOrganization.Description = P_oDbDataReader["RE_DESCRIPTION"].ToString();
                        l_oOrganization.Name = P_oDbDataReader["RE_NOM"].ToString();

                        //** FPE: Décommenter lorsque que toutes les procédures prendront en compte le parametre
                        //l_oOrganization.Deleted = (bool)FieldRow.Translate(P_oDbDataReader["RE_SUPPR"].ToString());

                        l_lstOrganization.Add(l_oOrganization);
                    }
                }
            }
            catch ( Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father)  + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstOrganization;
        }


        /// <summary>
        /// Translates the one organization.
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        public static Organization TranslateOneOrganization(IDataReader DbDataReader)
        {
            var organization = new Organization();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("OrganizationTranslator.TranslateOneOrganization (DbDataReader <" + DbDataReader.ToString() + ">)...");

            try
            {
                using (DbDataReader)
                {
                    while (DbDataReader.Read())
                    {
                        organization = new Organization
                        {
                            Identifier = DbDataReader.GetLongValue("RE_ID"),
                            Description = DbDataReader.GetStringValue("RE_DESCRIPTION"),
                            Name = DbDataReader.GetStringValue("RE_NOM"),
                            //Deleted = DbDataReader.GetBooleanValue("RE_SUPPR"),
                            //Rank = DbDataReader.GetIntValue("RE_RANK")
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return organization;
        } 
    }
}
